<?php
/**
 * Login controller
 *
 * @author den
 *
 */
class LoginController extends modules_default_controllers_ControllerBase
{
	protected $_bForLoggedUsersOnly = false;

//----------------------------------------------------------------------------------------------------

	public $ajaxable = array(
//		'index' => array('json'),
	);

//----------------------------------------------------------------------------------------------------

	public function init()
	{
		if (! $this->getRequest()->isXmlHttpRequest())
		{
			parent::init();

			$this->view->headLink()->appendStylesheet('/css/validation_css/screen.css');
	
			$this->view->headScript()->appendFile('/js/registration.js');
			$this->view->headScript()->appendFile('/js/jquery.validate.js');
		}

		$this->_helper->AjaxContext()->initContext('json');
	}

//----------------------------------------------------------------------------------------------------

	public function indexAction()
	{
//		$this->_helper->viewRenderer->setNoRender();
//		$this->_helper->layout->disableLayout();

		if (Lemar_User::isLogged())
		{
			$this->_redirect('/');
		}

		$oRequest     = $this->getRequest();
		$oForm        = new forms_LoginForm();
		$oZendSession = Zend_Registry::getInstance()->get('Zend_Session_Namespace');
		$errorMessage = $oZendSession->errorMessage;
		$oZendSession->errorMessage = '';
		$okRegister   = $oRequest->getParam('message');

		if ($oRequest->isPost())
		{
			$bRememberMe = ('on' == $oRequest->getParam('remember'));
			try
			{
				Lemar_User::doLogin($oForm, $oRequest->getPost(), $bRememberMe);
			}
			catch (Lemar_Exception $le)
			{
				$errorMessages = Lemar_User::getErrorMessages();
                foreach ($errorMessages as $sControlName => $sErrorIndexes)
                {
					$errorMessage[str_ireplace('#', '', $sErrorIndexes[0])] = $sErrorIndexes[1];
                }
				$oZendSession->errorMessage = $errorMessage;
			}
			$this->_redirect('/login/');
		}
		else
		{

		}

		$this->view->errorMessage = $errorMessage;
	}

//----------------------------------------------------------------------------------------------------


} // CLASS
